package com.zr.service.car.impl;

import com.zr.dao.car.CarInfoDao;
import com.zr.dao.car.CarPhotoDao;
import com.zr.dao.dispatch.DispatchTicketDao;
import com.zr.dao.order.OrderStatusDao;
import com.zr.service.car.CarMessageService;
import com.zr.vo.car.CarInfo;
import com.zr.vo.car.CarMessage;
import com.zr.vo.dispatch.DispatchTicket;
import com.zr.vo.order.OrderStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/***
 * 运输信息 服务实现层
 */
@Service
public class CarMessageServiceImpl implements CarMessageService {
    @Resource
    private CarInfoDao carInfoDao;
    @Resource
    private DispatchTicketDao dispatchTicketDao;
    @Resource
    private OrderStatusDao orderStatusDao;
    @Resource
    private CarPhotoDao carPhotoDao;

    /***
     * 获取最近更新的运输信息
     * @return
     */
    @Transactional
    @Override
    public List<CarMessage> listResent() {
        List<CarMessage> carMessageList=new ArrayList<>();
        List<OrderStatus> orderStatusList=orderStatusDao.listDispathMessages();
        for(OrderStatus orderStatus:orderStatusList){
            CarMessage carMessage=new CarMessage();
            //设置车辆位置
            if(orderStatus.getStatusCity()!=null){
                carMessage.setLocation(orderStatus.getStatusCity().getMergerName());
            }else{
                carMessage.setLocation("位置信息错误");
            }
            DispatchTicket dispatchTicket=dispatchTicketDao.selectByTicketId(orderStatus.getStatusTicketId());
            //设置调度单编号
            carMessage.setDispathId(dispatchTicket.getTicketId());
            //设置车牌号
            carMessage.setCarNumber(dispatchTicket.getCarNumber());
            CarInfo carInfo=carInfoDao.selectByNumber(dispatchTicket.getCarNumber());
            //设置司机姓名
            carMessage.setDirverName(carInfo.getAnotherOne());
            String carPhotoPath=carPhotoDao.selectHeadByNum(dispatchTicket.getCarNumber());
            String imgBase="";
            if(carPhotoPath==null){
                imgBase=CarServiceImpl.getImgBase("D:\\Img\\a.jpg");
            }else{
                imgBase=CarServiceImpl.getImgBase(carPhotoPath);
            }
            //写入车辆照片base64
            carMessage.setCarPhoto(imgBase);
            //写入状态时间
            carMessage.setCreateTime(orderStatus.getStatusTime());
            switch (orderStatus.getStatusName()){
                case "待提货": carMessage.setDispathStatus("已接单");break;
                case "待发车": carMessage.setDispathStatus("已提货");break;
                case "运输中": carMessage.setDispathStatus("已发车");break;
                case "调度单完成": carMessage.setDispathStatus("已完成");break;
                case "待调度": carMessage.setDispathStatus("已拒绝");break;
                default:break;
            }
            carMessageList.add(carMessage);
        }
        return carMessageList;
    }
}
